package controllers;
import business.BaseBiz;
import business.SysUser;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import utils.Convert;
import models.*;
import play.Logger;
import utils.ErrorInfo;
import utils.PageBean;

import java.util.List;

/**
 * 后台登录, 增删改 统一用 ajax, 查用 回发
 */
public class SystemCenter extends BaseController {
    //权限列表
    public static void adminRule() {
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlCount = new StringBuilder();
        sql.append("select * from sys_menu");
        sqlCount.append("select count(*) from sys_menu");
        PageBean<sys_menu> pageBean =new  BaseBiz<sys_menu>().queryList(sys_menu.class,sql,sqlCount);
        render(pageBean);
    }
    //权限保存
    public static void insert(){
        Logger.info("00000000000");
        String menu_name = params.get("menu_name");
        String access_url = params.get("access_url");
        sys_menu sysMenu = new sys_menu();
        sysMenu.menu_name = menu_name;
        sysMenu.access_url = access_url;
        sysMenu.save();
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.initSave();

        renderJSON(errorInfo);
    }
    //权限编辑，删除
    public static void adminRuleEdit(){
        String v = params.get("v");
        if(v.equals("view")){
            String id = params.get("id");
            sys_menu sysMenu = sys_menu.findById(Long.parseLong(id));
            render(sysMenu);
        }else if(v.equals("update")){
            String id = params.get("id");
            String menu_name = params.get("menu_name");
            String access_url = params.get("access_url");
            sys_menu sysMenu = sys_menu.findById(Long.parseLong(id));
            if(sysMenu!=null){
                sysMenu.menu_name = menu_name;
                sysMenu.access_url = access_url;
                sysMenu.save();
            }
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.initSave();
            renderJSON(errorInfo);
        }else if(v.equals("delete")){
            String[] strs=params.get("ids").split(",");
            for(int i=0;i<strs.length;i++){
                long id = Convert.strToLong(strs[i],0);
                Logger.info("id=" + id);
                if(id>0){
                    sys_menu.delete(" from sys_menu where  id=?1",id);
                }
            }
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.msg="删除成功";
            errorInfo.code=1;
            renderJSON(errorInfo);
        }
    }
    //角色管理
    public static void adminRole(){
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlCount = new StringBuilder();
        sql.append("select * from sys_role");
        sqlCount.append("select count(*) from sys_role");
        BaseBiz<sys_role> baseBiz = new  BaseBiz();
        PageBean<sys_role> pageBean =baseBiz.queryList(sys_role.class,sql,sqlCount);
        render(pageBean);
    }
    //添加，更新角色
    public static void roleEdit(String id,String v,String role_name){
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.initSave();
        if(v.equals("view")){
            if(!StrUtil.isEmpty(id)){
                sys_role sysRole = sys_role.findById(Long.parseLong(id));
                render(sysRole);
            }
           render();
        }else if(v.equals("update")){
            if(StrUtil.isEmpty(id)){
                sys_role sysRole = new sys_role();
                sysRole.role_name = role_name;
                sysRole.save();
            }else{
                sys_role sysRole = sys_role.findById(Long.parseLong(id));
                if(sysRole!=null){
                    sysRole.role_name = role_name;
                    sysRole.save();
                }
            }
            renderJSON(errorInfo);
        }else if(v.equals("delete")){
            String[] strs=params.get("ids").split(",");
            for(int i=0;i<strs.length;i++){
                long idL = Convert.strToLong(strs[i],0);
                Logger.info("idL=" + idL);
                if(idL>0){
                    sys_role.delete(" from sys_role where  id=?",idL);
                }
            }
            errorInfo.msg="删除成功";
            errorInfo.code=1;
            renderJSON(errorInfo);
        }
    }
    //管理员管理
    public static void adminList(){
        StringBuilder sql = new StringBuilder();
        StringBuilder sqlCount = new StringBuilder();
        sql.append("select * from sys_user");
        sqlCount.append("select count(*) from sys_user");
        BaseBiz<sys_user> baseBiz = new  BaseBiz();
        PageBean<sys_user> pageBean =baseBiz.queryList(sys_user.class,sql,sqlCount);
        render(pageBean);
    }
    //添加权限
    public static void addPermission(String id,String v,String roleId){
        if(!StrUtil.isEmpty(id)){
            if(v.equals("view")){
                List<sys_menu> sysMenuList= sys_menu.findAll();
                sys_role sysRole = sys_role.findById(Long.parseLong(id));
                List<sys_role_menu> sysRoleMenuList = sys_role_menu.find("from sys_role_menu where role_id=?1",Long.parseLong(id)).fetch();
                String menu_id="";
                if(sysRoleMenuList.size()>0){
                    for (sys_role_menu sysRoleMenu: sysRoleMenuList) {
                        menu_id += sysRoleMenu.menu_id + ",";
                    }
                    menu_id = menu_id.substring(0,menu_id.length()-1);
                }
                render(sysMenuList,sysRole,sysRoleMenuList,menu_id);
            }else if(v.equals("insert")){
                String[] strs = id.split(",");
                long roleId0 = Convert.strToLong(roleId,0);
                sys_role_menu.delete("role_id=?1",roleId0);
                for(int i=0;i<strs.length;i++){
                    int id0 = Convert.strToInt(strs[i],0);
                    if(id0>0){
                        sys_role_menu sysRoleMenu = new sys_role_menu();
                        sysRoleMenu.menu_id =   id0;
                        sysRoleMenu.role_id = roleId0;
                        sysRoleMenu.save();
                    }
                }
                ErrorInfo errorInfo = new ErrorInfo();
                errorInfo.initSave();
                renderJSON(errorInfo);
            }
        }
    }
    //管理员管理
    public static void adminEdit(String id,String v){
        if("state".equals(v)){
            sys_user sysUser = sys_user.findById(Long.parseLong(id));
            if(sysUser.status==1){
                sysUser.status = 0;
            }else{
                sysUser.status = 1;
            }
            sysUser.save();
            //随意输出个字符
            renderText("1");
        }else if("view".equals(v)){
            List<sys_role> sysRoleList = sys_role.findAll();
            render(sysRoleList);
            // 获取更新单个
        }else if(v.equals("updateView")){
            List<sys_role> sysRoleList = sys_role.findAll();
            sys_user sysUser = sys_user.findById(Long.parseLong(id));
            List<sys_user_role> sysUserRoleList = sys_user_role.find(" from sys_user_role where user_id=?1",Long.parseLong(id)).fetch();
            boolean initEdit = true;
            Gson gson = new Gson();
            String jsonStr = gson.toJson(sysUserRoleList);
            render(sysUser,sysRoleList,initEdit,jsonStr);
        }else if(v.equals("update")){
            SysUser.update(params);
            renderText("1");
        }else if(v.equals("insert")){
            ErrorInfo errorInfo = new ErrorInfo();
            String username = params.get("username");
            if(StrUtil.isEmpty(username)){
                errorInfo.code = -1;
                errorInfo.msg="用户名不能为空";
                renderJSON(errorInfo);
            }
            long cntUser = sys_user.count(" from sys_user where username=?1",username);
            if(cntUser>0){
                errorInfo.code = -2;
                errorInfo.msg="用户名已存在";
                renderJSON(errorInfo);
            }
            SysUser.update(params);
            errorInfo.initSave();
            renderJSON(errorInfo);
        }else if(v.equals("delete")){
            ErrorInfo errorInfo = new ErrorInfo();
            String[] split=id.split(",");
            for(int i=0;i<split.length;i++){
                String id0 =split[i];
                if(!SysUser.isAdmin(Long.parseLong(id0))){
                    sys_user.delete("from sys_user where id=?1",Long.parseLong(id0));
                    sys_user_role.delete("from sys_user_role where user_id=?1",Long.parseLong(id0));
                }else{
                    errorInfo.code = -1;
                    errorInfo.msg = "删除失败:管理员不能删除";
                    renderJSON(errorInfo);
                }
            }
            errorInfo.code = 1;
            errorInfo.msg = "删除成功";
            renderJSON(errorInfo);
        }
    }
}
